package com.tom.architect.day15.simple4;

/**
 * Created by hcDarren on 2017/10/14.
 * 数据库的更新
 */
public class DatabaseManager {
    //region 单例模式
    private static volatile DatabaseManager mInstance;

    // 观察者列表
    private Observable<Member, Observer<Member>> mObservable;
    private DatabaseManager() {
        mObservable = new Observable<>();
    }
    public static DatabaseManager getInstance(){
        if(mInstance == null){
            synchronized (DatabaseManager.class){
                if(mInstance == null){
                    mInstance = new DatabaseManager();
                }
            }
        }
        return mInstance;
    }
    //endregion


    // 新增插入一条数据
    public void insert(Member member){
        // TODO 插入数据库,省略了....

        // 通知观察者
        mObservable.addUpdate(member);
    }

    // 注册监听
    public void register(Observer<Member> observer){
        mObservable.register(observer);
    }

    // 取消监听
    public void unregister(Observer<Member> observer){
        mObservable.unregister(observer);
    }

}
